package com.turman.builibrary.banner.core;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import java.util.List;

public class BBannerAdapter extends PagerAdapter {
    private Context mContext;
    private SparseArray<BBannerViewHolder> mChachedViews = new SparseArray<>();
    private IBBanner.OnBannerClickListener mBannerClickListener;
    private IBindAdapter mBindAdapter;
    private List<? extends BBannerModel> models;

    //是否自动播放
    private boolean autoPlay = true;
    //非自动播放状态下是否循环切换
    private boolean mLoop = false;

    private int mLayoutResId = -1;

    public BBannerAdapter(Context context) {
        this.mContext = context;
    }

    public void setBannerData(@NonNull List<? extends BBannerModel> models) {
        this.models = models;
        initCachedViews();
        notifyDataSetChanged();
    }

    public void setOnBannerClickListener(IBBanner.OnBannerClickListener bannerChickListener) {
        this.mBannerClickListener = bannerChickListener;
    }

    public void setLayoutResId(@LayoutRes int layoutResId) {
        this.mLayoutResId = layoutResId;
    }

    public void setAutoPlay(boolean autoPlay) {
        this.autoPlay = autoPlay;
    }

    public void setLoop(boolean loop) {
        this.mLoop = loop;
    }

    public int getRealCount() {
        return models == null ? 0 : models.size();
    }

    public void setBindAdapter(IBindAdapter bindAdapter) {
        this.mBindAdapter = bindAdapter;
    }

    private void initCachedViews() {
        mChachedViews = new SparseArray<>();
        for (int i = 0; i < models.size(); i++) {
            BBannerViewHolder viewHolder = new BBannerViewHolder(createView(LayoutInflater.from(mContext), null));
            mChachedViews.put(i,viewHolder);
        }
    }

    private View createView(LayoutInflater from, ViewGroup parent) {
        if (mLayoutResId == -1) {
            throw new IllegalArgumentException("you must be set setLayoutResId fitst");
        }
        return from.inflate(mLayoutResId, parent, false);
    }

    public int getFirstItem() {
        //这里是为了配合instantiateItem方法中realPosition = position % getRealCount();
        // - (Integer.MAX_VALUE / 2) % getRealCount()的主要目的是用于获取realPosition=0的位置
        return Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2) % getRealCount();
    }


    @Override
    public int getCount() {
        //自动播放和无限滚动是返回数量MAX_VALUE
        return autoPlay ? Integer.MAX_VALUE : (mLoop ? Integer.MAX_VALUE : getRealCount());
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        int realPosition = position;
        if (getRealCount() > 0) {
            realPosition = position % getRealCount();
        }
        BBannerViewHolder holder = mChachedViews.get(realPosition);
        if (container.equals(holder.rootView.getParent())) {
            container.removeView(holder.rootView);
        }

        onBind(holder, models.get(realPosition), realPosition);
        if (holder.rootView.getParent() != null) {
            ((ViewGroup) holder.rootView.getParent()).removeView(holder.rootView);
        }
        container.addView(holder.rootView);
        return holder.rootView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
    }

    public void onBind(@NonNull BBannerViewHolder holder, BBannerModel bBannerModel, int realPosition) {
        holder.rootView.setOnClickListener(v -> {
            if (mBannerClickListener != null) {
                mBannerClickListener.onBannerClick(holder, bBannerModel, realPosition);
            }
        });
        if (mBindAdapter != null) {
            mBindAdapter.onBind(holder, bBannerModel, realPosition);
        }
    }


    public static class BBannerViewHolder {
        private SparseArray<View> viewSparseArray;
        View rootView;

        public BBannerViewHolder(View rootView) {
            this.rootView = rootView;
        }

        public View getRootView() {
            return rootView;
        }

        public <V extends View> V findViewById(int id) {
            if (!(rootView instanceof ViewGroup)) {
                return (V) rootView;
            }
            if (this.viewSparseArray == null) {
                this.viewSparseArray = new SparseArray<>();
            }

            V view = (V) viewSparseArray.get(id);
            if (view == null) {
                view = rootView.findViewById(id);
                viewSparseArray.put(id, view);
            }
            return view;
        }
    }
}
